/*
    cocos封装的公用方法和第三方平台需要初始化的函数
*/
var api=require("./thirdapi");
var config=require("./config");
var yqd=require("./yqdapi");
export default{
    winSize:null,//系统尺寸
    puzzle_list:[],//拼图图片列表总数组
    addpuzzle_list:[],//index翻页或者puzzlist翻页后增加的数组
    page:1,//拼图列表的当前页数
    count:9,//每页的条数
    puzzle_null:false,//没有更多数据
    list:[],//列表
    listpage:1,//拼图列表的当前页数
    listcount:9,//每页的条数
    list_null:false,//没有更多数据
    unloadJili:false,//激励视频未加载
    pageinfo: {//配置的流量主
        banner: null,
        jili: null,
        tiepian: null,
        chapin: null,
        kefuid: null,
        kefuimg: null,
        changeimg: null,
        alertimg: null,
        gotoappid: null,
        gotoapppath: null,
        zbsqid: null,
    },
    /*
        @onLaunch加载必须先运行的周期函数
    */
    onLaunch(gameCtl){
        var that=this;
        this.gameCtl=gameCtl;
        api.showShareMenu();
        this.getPageInfo(function(res){
            if(that.pageinfo.jili){
                that.setJili(that.pageinfo.jili)
            }
        })

        this.getMinList({
            success(res){
                if(res.data.data){//如果有数据
                    that.list=that.list.concat(res.data.data);
                    that.gameCtl.listAlert.init(that.gameCtl)
                }
            },
            fail(res){
                that.gameCtl.listAlert.active=false;
            }
        });
        
    },
    /*
        @getMinList获取后台列表
    */
    getMinList(param){
        var that=this;
        if(that.list_null){return;}
        yqd.getMinList({
            page:that.listpage,
            count:that.listcount,
            success(res){
                console.log("listres",res)
                if(res.data.data){
                    that.listpage++;
                     param.success&&param.success(res);
                }else{
                    that.list_null=true;
                    param.fail&&param.fail(res);
                }
            },
            fail(res){
                console.log("failres",res)
                that.list_null=true;
                param.fail&&param.fail(res);

            }
        })
    },
    /*
        @setJili初始化激励视频
    */
    setJili(adUnitId){
        var that=this;
        api.createRewardedVideoAd(adUnitId,res=>{
            console.log("视频res",res)
            if (res && res.isEnded || res === undefined) {
                // 正常播放结束，下发奖励
                console.log("播放成功")
                that.gameCtl.sendvideoRes(1)
            } else {
                console.log("播放取消")
                that.gameCtl.sendvideoRes(2)

            }
        },res=>{
            console.log("视频加载失败",res)
            that.unloadJili=true;

        })
    },


    getPageInfo(fn) {//获取流量主信息
        var that = this;
        yqd.getAd({
            place_id:14,
            success(res){
                console.log("res",res)
                if (res.data.status == 1001 && res.data.set[0].ad_type == 6) {
                    var bannerinfo = res.data.set[0];
                    that.pageinfo.kefuid = bannerinfo.kf_id;
                    that.pageinfo.kefuimg = bannerinfo.kf_img;
                    that.pageinfo.alertimg = bannerinfo.ad_img;
                    that.pageinfo.gotoappid = bannerinfo.appid;
                    that.pageinfo.gotoapppath = bannerinfo.ad_path;
                    that.pageinfo.zbsqid = bannerinfo.fiction_game_id;
                    that.pageinfo.changeimg = bannerinfo.kf_img;
                    if (bannerinfo.ad_fu_title) {
                        config.wx_name = bannerinfo.ad_fu_title;
                    }
                    if (bannerinfo.ad_liuliang) {
                        // var adlist=bannerinfo.ad_desc.split("\n");
                        var adlist = bannerinfo.ad_liuliang;
                        // var adlist=["adunit-5395cc20d5230312","adunit-abea7a8aec09c6bc","adunit-a1a158d208caaa65","adunit-16d5041a689cfad1"];
                        that.pageinfo.banner = adlist[0];
                        that.pageinfo.jili = adlist[1];
                        that.pageinfo.tiepian = adlist[2];
                        that.pageinfo.chapin = adlist[3];
                    }
                    console.log("pageinfo", that.pageinfo)
                }
                fn&&fn(res)
                
            },
            fail(res){
                fn&&fn(res)

            }
        })
    },

    
    /*
        @downloadImg网络图片
            url网络图片地址
            success(res):接口调用成功返回值为cocos的Texture2D
            fail(res):接口调用失败返回
    */
    downloadImg(param){
        cc.loader.load(param.url, function (err, Texture2D) {
            // _this.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(Texture2D);
            // console.log('Texture2D',Texture2D)
            // console.log('err',err)
            if(err){
                param.fail&&param.fail(err)
            }else{
                param.success&&param.success(Texture2D)
            }
        });
    },


    /*
        @cutPicture截取原始图片
            data:puzzle_texture2D,
            x:截取原始图的x坐标
            y:截取原属土的y坐标
            width:截取原始图的宽度
            height:截取原始图的高度
    */
    cutPicture(data,x,y,width,height){//截取图片
        // console.log(x,y)
        var frame;
        if(data instanceof cc.SpriteFrame) {
            // console.log("是SpriteFrame")
            frame=data;
        }else if(data instanceof cc.Texture2D) {
            // console.log("是Texture2D")
            frame = new cc.SpriteFrame(data);
        }
        if(!frame) {
            return null;
        }
        //设置显示区域 ，注意使用cc.Rect()会得到undefinde 
        frame.setRect(cc.rect(x,y,width,height));
            // console.log(frame)
        return frame;
    },
}